﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using Entities.IRepository;
using BLL;
using DAL;
using DevComponents.DotNetBar;

namespace HeThongNhaSach
{
	public partial class frmQLTaiKhoan : DevComponents.DotNetBar.Office2007Form
	{
        public frmMain frmChinh = new frmMain();
		GeneralRepository<TaiKhoan> _TKRepo;
		QLTaiKhoan _TKManager;
		public frmQLTaiKhoan()
		{
			InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(btnThoat_Click);

		}
        private void frmQLTaiKhoan_Load(object sender, EventArgs e)
        {
            _TKRepo = new GeneralRepository<TaiKhoan>();
            _TKManager = new QLTaiKhoan(_TKRepo);
            cbTenDN.DataSource = _TKRepo.GetAll().ToList();
            cbTenDN.ValueMember = "Username";
            cbTenDN.DisplayMember = "Username";
            if (cbTenDN.SelectedItem != null)
            {
                TaiKhoan tk = cbTenDN.SelectedItem as TaiKhoan;
                txtPass.Text = tk.Password;
                cbPhanQuyen.Text = tk.PhanQuyen;
                cbTinhTrang.Text = tk.TrangThai;

            }
            else
            {
                txtPass.Text = "";
                cbPhanQuyen.SelectedItem = 0;
                cbTinhTrang.SelectedItem = 0;
            }
        }
		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			TaiKhoan tk = new TaiKhoan();
			tk.Username = cbTenDN.Text;
			tk.Password = txtPass.Text;
			tk.PhanQuyen = cbPhanQuyen.Text;
			tk.TrangThai = cbTinhTrang.Text;
			var vResult = _TKManager.Validate(tk);
			if (vResult.Valid)
			{
				var aResult = _TKManager.Update(tk);
				if (aResult.Success)
				{
				}
				else
					MessageBox.Show(aResult.GetErrorMessages());
                MessageBox.Show("Cap nhập thành công");
			}
			else MessageBox.Show(vResult.ToString());
            this.Close();
            frmChinh.datataikhoan.DataSource = _TKRepo.GetAll().ToList();
		}

		private void btnXoa_Click(object sender, EventArgs e)
		{
			if (cbPhanQuyen.Text == "Quản Lý")
				MessageBox.Show("Không thể xóa tài khoản quản lý");
			else
			{
				TaiKhoan tk = _TKRepo.GetAll().ToList().Find(delegate(TaiKhoan t)
				{
					return t.Username == cbTenDN.Text;
				});
				if (tk != null)
				{
					var vResult = _TKManager.Validate(tk);
					if (vResult.Valid)
					{
						var aResult = _TKManager.Delete(tk);
						if (!aResult.Success)
							MessageBox.Show(aResult.GetErrorMessages());
					}
					else MessageBox.Show(vResult.ToString());
					this.Close();
                    frmChinh.datataikhoan.DataSource = _TKRepo.GetAll().ToList();
				}
				else
				{
					MessageBox.Show("Xóa tài khoản không thành công");
                    
				}
			}
		}

		private void btnThem_Click(object sender, EventArgs e)
		{
			TaiKhoan tk = new TaiKhoan();
			tk.Username = cbTenDN.Text;
			tk.Password = txtPass.Text;
			tk.PhanQuyen = cbPhanQuyen.Text;
			tk.TrangThai = cbTinhTrang.Text;
			var vResult = _TKManager.Validate(tk);
			if (vResult.Valid)
			{
				var aResult = _TKManager.Insert(tk);
				if (aResult.Success)
				{

				}
				else

					MessageBox.Show(aResult.GetErrorMessages());
                MessageBox.Show("Thêm thành công");
                
			}
			else MessageBox.Show(vResult.ToString());

            this.Close();
            frmChinh.datataikhoan.DataSource = _TKRepo.GetAll().ToList();
           
		}

		

		private void cbPhanQuyen_SelectedIndexChanged(object sender, EventArgs e)
		{

		}
	}
}